package com.gxuwz.restaurant.controller;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.restaurant.common.Result;
import com.gxuwz.restaurant.entity.Quhao;
import com.gxuwz.restaurant.entity.SeatManage;
import com.gxuwz.restaurant.mapper.QuhaoMapper;
import com.gxuwz.restaurant.mapper.SeatManageMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Quhao")
public class QuhaoController {
    int count=0;
    @Resource
    QuhaoMapper quhaoMapper;
    @Resource
    SeatManageMapper seatManageMapper;



    @PostMapping("/quhao")   //get用来查询
    public Integer quhao(@RequestBody JSONObject req) {


        String  nickName =req.getStr("nickName");
        String  avatarUrl =req.getStr("avatarUrl");
        QueryWrapper<Quhao> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_name",nickName);
        queryWrapper.eq("customer_avatar",avatarUrl);

         Quhao quhao1  = quhaoMapper.selectOne(queryWrapper);
        System.out.println(quhao1);
        if(quhao1!=null){
            System.out.println("请勿重复取号");
             return -1;
         }else {
             ++count;
             Quhao quhao =new Quhao();
             quhao.setNumber(count);
             quhao.setCustomerAvatar(avatarUrl);
             quhao.setCustomerName(nickName);
             quhao.setState("未入场");
             quhaoMapper.insert(quhao);
            Quhao quhao2  = quhaoMapper.selectOne(queryWrapper);
            Integer number = quhao2.getNumber();
            System.out.println("取号成功，你的 号码是"+number);
             return number;
         }


    }

    @PostMapping("/load")   //get用来查询
    public Integer load(@RequestBody JSONObject req) {


        String  nickName =req.getStr("nickName");
        QueryWrapper<Quhao> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_name",nickName);
        Quhao quhao1  = quhaoMapper.selectOne(queryWrapper);
            return quhao1.getNumber();
    }
    @PostMapping("/Enter")   //get用来查询
    public Integer Enter(@RequestBody JSONObject req) {


        String  nickName =req.getStr("nickName");
        QueryWrapper<Quhao> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_name",nickName);
        Quhao quhao =new Quhao();
        quhao.setState("已入场");
        quhaoMapper.update(quhao,queryWrapper);
        return 1;
    }

    @GetMapping("/loadQuhao")   //get用来查询
    public Result<?> findPageQuhao(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                                   @RequestParam(defaultValue = "") String state){
        System.out.println("执行查询");
        LambdaQueryWrapper<Quhao> wrapper = Wrappers.<Quhao>lambdaQuery();
        if(StrUtil.isNotBlank(search)){
            wrapper.like(Quhao::getNumber,search);
        }
        wrapper.eq(Quhao::getState,state);
        Page<Quhao> quhaoPage =  quhaoMapper.selectPage(new Page<>(pageNum,pageSize), wrapper);


        return Result.success(quhaoPage );

    }
    @PostMapping("/getState")   //get用来查询
    public Integer getState(@RequestBody JSONObject req1){
        String  nickName =req1.getStr("nickName");
        QueryWrapper<Quhao> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_name",nickName);
        Quhao quhao = quhaoMapper.selectOne(queryWrapper);
        if (quhao.getState().equals("可入场")){
            return 1;
        }else {
            return 0;
        }
}

    @PostMapping //post用来新增
    public Result<?> save(@RequestBody Quhao quhao){


        quhaoMapper.insert(quhao);

        return Result.success();
    }
    @DeleteMapping("/{id}") //DELETE用来删除
    public Result<?> delete(@PathVariable Integer id){
        Quhao quhao=new Quhao();
        quhao.setId(id);
        quhao.setState("可入场");

        quhaoMapper.updateById(quhao);
        return Result.success();
    }
    @DeleteMapping("/delete/{id}") //DELETE用来删除
    public Result<?> delete1(@PathVariable Integer id){


        quhaoMapper.deleteById(id);
        return Result.success();
    }

    @PutMapping //put用来更新
    public Result<?> update(@RequestBody Quhao quhao){
            quhaoMapper.updateById(quhao);
            return Result.success();
        }


        @GetMapping("/loadSeat")
    public Result<?>loadSeat(){
        QueryWrapper<SeatManage> queryWrapper =new QueryWrapper();

        queryWrapper.eq("seat_state","true");
            queryWrapper.eq("cleanstate","打扫完成");
            List<SeatManage> seatManages = seatManageMapper.selectList(queryWrapper);
            if (seatManages==null){
                System.out.println(seatManages+"ggg");
                return Result.success(null);
            }else {
                System.out.println(seatManages+"333");
                List list=new ArrayList();
                for( SeatManage seatManage:seatManages){
                            list.add(seatManage.getSeatNumber());
                }
                return Result.success(list);
            }

        }

    }
